<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="请选择签约产品">
        <el-select v-model="form.OrderListID">
          <el-option
            v-for="item in OrderList"
            :key="item.OrderListID"
            :label="item.ProductName"
            :value="item.OrderListID"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="指定店铺/账号">
        <el-select v-model="form.BindStoreID">
          <el-option
            v-for="item in StoreList"
            :key="item.BindStoreID"
            :label="item.BindStoreAccount"
            :value="item.BindStoreID"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="title">
        <el-select v-model="form.toStaffID">
          <el-option
            v-for="item in StaffOptions"
            :key="item.StaffID"
            :label="item.StaffName"
            :value="item.StaffID"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { AssignStaffSelect } from '@/api/common'
  import { saveOrderAssign } from '@/api/businessReviewsplitorders'

  export default defineComponent({
    name: 'AssignOperate',
    setup() {
      const $baseMessage = inject('$baseMessage')
      const { fetchData } = inject('rootMethods')

      const state = reactive({
        OrderID: 0,
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
        OrderList: [],
        StoreList: [],
        StaffOptions: [],
        getStaffParams: [],
      })

      const showEdit = (orderid, totype, OrderList, StoreList, row) => {
        state.OrderID = orderid
        state.OrderList = OrderList
        state.StoreList = StoreList
        if (totype == 1) {
          state.title = '分单到运营内勤'
          state.form = Object.assign({}, row)
          state.form.OrderID = orderid
          state.form.TaskTarget = totype
          state.form.TaskTargetName = '运营内勤'
          state.getStaffParams = [10]
        } else if (totype == 2) {
          state.title = '分单到运营店长'
          state.form = Object.assign({}, row)
          state.form.OrderID = orderid
          state.form.TaskTarget = totype
          state.form.TaskTargetName = '运营店长'
          state.getStaffParams = [20]
        } else if (totype == 3) {
          state.title = '指定运营师'
          state.form = Object.assign({}, row)
          state.form.OrderID = orderid
          state.form.TaskTarget = totype
          state.form.TaskTargetName = '运营师'
          state.getStaffParams = [21]
        } else if (totype == 4) {
          state.title = '指定美工内勤'
          state.form = Object.assign({}, row)
          state.form.OrderID = orderid
          state.form.TaskTarget = totype
          state.form.TaskTargetName = '美工内勤'
          state.getStaffParams = [11]
        } else if (totype == 5) {
          state.title = '指定视频人员'
          state.form = Object.assign({}, row)
          state.form.OrderID = orderid
          state.form.TaskTarget = totype
          state.form.TaskTargetName = '视频人员'
          state.getStaffParams = [5]
        } else if (totype == 6) {
          state.title = '分配到美工'
          state.form = Object.assign({}, row)
          state.form.OrderID = orderid
          state.form.TaskTarget = totype
          state.form.TaskTargetName = '综合主管'
          state.getStaffParams = [46]
        } else if (totype == 7) {
          state.title = '分配到综合主管'
          state.form = Object.assign({}, row)
          state.form.OrderID = orderid
          state.form.TaskTarget = totype
          state.form.TaskTargetName = '网站人员'
          state.getStaffParams = [46]
        } else if (totype == 8) {
          state.title = '分配程序开发人员'
          state.form = Object.assign({}, row)
          state.form.OrderID = orderid
          state.form.TaskTarget = totype
          state.form.TaskTargetName = '程序开发人员'
          state.getStaffParams = [49, 50]
        }

        getStaffSelect()
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveOrderAssign(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            fetchData()
            close()
          }
        })
      }

      const getStaffSelect = async () => {
        state.listLoading = true
        const getStaffParams = {
          StaffPostID: state.getStaffParams,
        }
        const {
          data: { StaffOptions },
        } = await AssignStaffSelect(getStaffParams)
        state.StaffOptions = StaffOptions
        state.listLoading = false
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
